<?php

namespace addons\shopro\controller;

use think\addons\Controller;
use addons\shopro\exception\Exception;
use addons\shopro\model\User;
use addons\shopro\model\Order as OrderModel;
use addons\shopro\model\OrderItem as OrderItemModel;
use addons\shopro\model\OrderAction as OrderActionModel;
use addons\shopro\model\GoodsComment as GoodsCommentModel;
use think\Db;

class Cron extends Controller
{

    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    
    // 订单自动失效
    public function orderInvalid () {
        \Think\Log::write('orderInvalid');

        // 超过十五分钟的订单
        $orders = OrderModel::nopay()->where('createtime', '<', (time() - 900))->select();

        
        foreach ($orders as $key => $order) {
            $order = Db::transaction(function () use ($order) {
                // 自动失效
                $order->status = OrderModel::STATUS_INVALID;        // 失效订单
                $order->save();

                OrderActionModel::operAdd($order, null, null, 'system', '系统自动失效订单');

                // 返还用户优惠券，积分
                $order = OrderModel::done($order);
                return $order;
            });
        }

        echo '订单失效执行成功';
    }


    // 订单自动确认收货
    public function orderConfirm()
    {
        // 未确认收货的订单
        $orders = OrderModel::payed()->noget()->select();

        foreach ($orders as $key => $order) {
            foreach ($order['item'] as $item) {
                // 查询 item 的发货时间 大于 10 天的
                $orderAction = OrderActionModel::where('order_id', $order['id'])
                                ->where('order_item_id', $item['id'])
                                ->where('order_status', OrderModel::STATUS_PAYED)
                                ->where('dispatch_status', OrderItemModel::DISPATCH_STATUS_SENDED)
                                ->where('aftersale_status', OrderItemModel::AFTERSALE_STATUS_NOAFTER)
                                ->where('refund_status', OrderItemModel::REFUND_STATUS_NOREFUND)
                                ->where('createtime', '<', (time() - 864000))
                                ->find();

                if ($orderAction) {
                    // 如果一次查询待收货订单过多，为避免重复确认收货，重新查询当前 item 已发货，未售后
                    $item = OrderItemModel::where('id', $item['id'])
                                ->where('dispatch_status', OrderItemModel::DISPATCH_STATUS_SENDED)
                                ->where('aftersale_status', OrderItemModel::AFTERSALE_STATUS_NOAFTER)
                                ->where('refund_status', OrderItemModel::REFUND_STATUS_NOREFUND)
                                ->find();
                    if ($item) {
                        $order = Db::transaction(function () use ($order, $item) {
                            $item->dispatch_status = OrderItemModel::DISPATCH_STATUS_GETED;        // 确认收货
                            $item->save();
    
                            OrderActionModel::operAdd($order, $item, null, 'system', '系统自动确认收货');
                        });
                    }
                }
            }
        }

        echo '订单自动收货执行成功';
    }



    public function orderComment()
    {
        // 确认收货 7 天自动评价
        $orders = OrderModel::payed()->nocomment()->select();

        foreach ($orders as $key => $order) {
            foreach ($order['item'] as $item) {
                // 查询 item 的收货时间 大于 7 天的
                $orderAction = OrderActionModel::where('order_id', $order['id'])
                    ->where('order_item_id', $item['id'])
                    ->where('order_status', OrderModel::STATUS_PAYED)                       // 已支付
                    ->where('dispatch_status', OrderItemModel::DISPATCH_STATUS_GETED)       // 已收货
                    ->where('comment_status', OrderItemModel::COMMENT_STATUS_NO)            // 未评价
                    ->where('aftersale_status', OrderItemModel::AFTERSALE_STATUS_NOAFTER)   // 未售后
                    ->where('refund_status', OrderItemModel::REFUND_STATUS_NOREFUND)        // 未退款
                    ->where('createtime', '<', (time() - 604800))       // 七天
                    ->find();

                if ($orderAction) {
                    // 如果一次查询待评价订单过多，为避免重复评价，重新查询当前 item 已发货，未售后
                    $item = OrderItemModel::where('id', $item['id'])
                        ->where('dispatch_status', OrderItemModel::DISPATCH_STATUS_GETED)
                        ->where('comment_status', OrderItemModel::COMMENT_STATUS_NO) 
                        ->where('aftersale_status', OrderItemModel::AFTERSALE_STATUS_NOAFTER)
                        ->where('refund_status', OrderItemModel::REFUND_STATUS_NOREFUND)
                        ->find();

                    if ($item) {
                        $order = Db::transaction(function () use ($order, $item) {
                            GoodsCommentModel::create([
                                'goods_id' => $item->goods_id,
                                'order_id' => $order->id,
                                'user_id' => $order->user_id,
                                'level' => 5,           // 自动好评
                                'content' => '系统自动评价',
                                'images' => '',
                                'status' => 'show'
                            ]);

                            $item->comment_status = OrderItemModel::COMMENT_STATUS_OK;        // 评价成功
                            $item->save();

                            OrderActionModel::operAdd($order, $item, null, 'system', '系统自动评价成功');

                            // 判断并完成订单
                            $order = OrderModel::commentDone($order);
                            return $order;
                        });
                    }
                }
            }
        }

        echo '订单自动评价执行成功';
    }
}
